<script lang="ts" setup>
import { useRouter } from "vue-router"
import { storeToRefs } from "pinia"
import { useAppStore } from "@/store/modules/app"
import { useSettingsStore } from "@/store/modules/settings"
import { useUserStore } from "@/store/modules/user"
import { UserFilled } from "@element-plus/icons-vue"
import Hamburger from "../Hamburger/index.vue"
import Breadcrumb from "../Breadcrumb/index.vue"
import ThemeSwitch from "@/components/ThemeSwitch/index.vue"
import Screenfull from "@/components/Screenfull/index.vue"
import Notify from "@/components/Notify/index.vue"
import { Download, Printer } from "@element-plus/icons-vue"

const router = useRouter()
const appStore = useAppStore()
const settingsStore = useSettingsStore()
const userStore = useUserStore()

const { sidebar } = storeToRefs(appStore)
const { showNotify, showThemeSwitch, showScreenfull } = storeToRefs(settingsStore)
showThemeSwitch.value = false
/** 切换侧边栏 */
const toggleSidebar = () => {
  appStore.toggleSidebar(false)
}
/** 登出 */
const logout = () => {
  userStore.logout()
  router.push({ path: "/login" })
}
const goBack = () => {
  router.push({ path: "/home" })
}
const fullscreenTips = "下载使用手册"
const downloadDoc = () => {
  window.location.href = "/doc/20231029-v1.pdf"
}

const username = localStorage.getItem("username") ?? "游客"
</script>

<template>
  <div class="navigation-bar">
    <Hamburger :is-active="sidebar.opened" class="hamburger" @toggle-click="toggleSidebar" />
    <Breadcrumb class="breadcrumb" />
    <div class="right-menu">
      <el-tooltip effect="dark" :content="fullscreenTips" placement="bottom">
        <el-icon class="mr-2 scale-135 cursor-pointer" @click="downloadDoc">
          <Printer />
        </el-icon>
      </el-tooltip>

      <Screenfull v-if="showScreenfull" class="right-menu-item" />
      <ThemeSwitch v-if="showThemeSwitch" class="right-menu-item" />
      <Notify v-if="showNotify" class="right-menu-item" />
      <el-dropdown class="right-menu-item">
        <div class="right-menu-avatar">
          <el-avatar :icon="UserFilled" :size="30" />
          <span>{{ userStore.username }}</span>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item divided @click="goBack">
              <span style="display: block">返回主页</span>
            </el-dropdown-item>
            <a v-if="username == 'admin'" target="_blank" href="/userManage/index">
              <el-dropdown-item>用户管理</el-dropdown-item>
            </a>
            <a v-if="username == 'admin'" target="_blank" href="/fileConfig/index">
              <el-dropdown-item>文件管理</el-dropdown-item>
            </a>
            <a v-if="username == 'admin'" target="_blank" href="/auth/index">
              <el-dropdown-item>授权管理</el-dropdown-item>
            </a>

            <el-dropdown-item divided @click="logout">
              <span style="display: block">退出登录</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <section class="username">{{ username }}</section>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.navigation-bar {
  height: var(--v3-navigationbar-height);
  overflow: hidden;
  background: var(--v3-header-bg-color);

  .hamburger {
    display: flex;
    align-items: center;
    height: 100%;
    float: left;
    padding: 0 15px;
    cursor: pointer;
  }

  .breadcrumb {
    float: left;

    // 参考 Bootstrap 的响应式设计将宽度设置为 576
    @media screen and (max-width: 576px) {
      display: none;
    }
  }

  .right-menu {
    float: right;
    margin-right: 10px;
    height: 100%;
    display: flex;
    align-items: center;
    color: #606266;

    .right-menu-item {
      padding: 0 10px;
      cursor: pointer;

      .right-menu-avatar {
        display: flex;
        align-items: center;

        .el-avatar {
          margin-right: 10px;
        }

        span {
          font-size: 16px;
        }
      }
    }
  }
}
</style>
